package com.njitzx.controller;


import cn.hutool.http.HttpResponse;
import cn.hutool.http.server.HttpServerResponse;
import com.njitzx.annotation.Log;
import com.njitzx.en.BusinessType;
import com.njitzx.entity.PageMemberVO;
import com.njitzx.entity.po.Member;
import com.njitzx.service.MemberService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.njitzx.entity.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;


import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;


/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 周鑫
 * @since 2024-08-20
 */
@Api(tags = "会员表接口")
@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {

    private final MemberService memberService;

    @Log(title = "查询会员信息",businessType = BusinessType.OTHER)
    @ApiOperation("会员表分页查询")
    @GetMapping
    public Result findPage(@ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
                           @ApiParam("页大小") @RequestParam(defaultValue = "5") Integer pageSize,
                           @ApiParam("查询条件") Member member) {
        System.out.println(member);
        PageMemberVO pageMemberVO=memberService.findPage(pageNum, pageSize, member);
        return Result.success(pageMemberVO);
    }
    @Log(title = "查询单个会员信息",businessType = BusinessType.OTHER)
    @ApiOperation("根据主键查询会员表")
    @GetMapping("/{id}")
    public Result findById(@PathVariable Serializable id) {
        return Result.success(memberService.findById(id));
    }
    @Log(title = "新增会员信息",businessType = BusinessType.INSERT)
    @ApiOperation("新增会员表")
    @PostMapping
    public Result add(@RequestBody Member member) {
        memberService.add(member);
        return Result.success();
    }
    @Log(title = "修改会员信息",businessType = BusinessType.UPDATE)
    @ApiOperation("修改会员表")
    @PutMapping("/{id}")
    public Result modify(@PathVariable Serializable id,@RequestBody Member member) {
        memberService.modify(member);
        return Result.success();
    }

    @Log(title = "查询会员信息",businessType = BusinessType.DELETE)
    @ApiOperation("根据主键删除会员表")
    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id) {
        memberService.removeById(id);
        return Result.success();
    }

    @ApiOperation("批量删除会员")
    @DeleteMapping
    public Result removeByIds(@RequestParam("ids") String ids) {
        List<Integer> collect = null;
        try {
            collect = Arrays.stream(ids.split(","))
                    .map(Integer::parseInt)
                    .collect(Collectors.toList());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println(ids);
        memberService.removeByIds(collect);
        return Result.success();
    }

    //POI操作excel文件
    @ApiOperation("导出会员信息")
    @GetMapping("/export")
    public void export(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize, Member member, HttpServletResponse resp) throws IOException {
        //导出所有的选学生新信息
        Workbook workbook = memberService.export(pageNum, pageSize, member);
        // 设置响应内容
        resp.setContentType("application/vnd.ms-excel");
        //设置响应头
        resp.setHeader("Content-Disposition", "attachment;filename=" + new String("会员信息表.xlsx".getBytes("utf-8"), "iso8859-1"));

        workbook.write(resp.getOutputStream());
    }

    @ApiOperation("导入会员信息")
    @PostMapping("/import")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws IOException {
//        System.out.println(file.getSize());
        memberService.importFile(file.getInputStream());
        return Result.success();
    }
}
